﻿Shader "Custom/Color"
{
    Properties
    {
        _Color ("色调", Color) = (1,1,1,1)
        [NoScaleOffset]_MainTex ("固有色贴图", 2D) = "white" {}
        [NoScaleOffset][Normal] _BumpMap("法线", 2D) = "bump" {}
        _BumpScale("法线强度", Float) = 1.0
        [NoScaleOffset]_GMA("三合一R:光滑度 G:金属度 B:AO", 2D) = "white" {}
        _Glossiness ("光滑度", Range(0,1)) = 0.0
        _Metallic ("金属度", Range(0,1)) = 0.0
        [Header(Edge light)]
        [Space]
        [HDR]_EdgeLightColor("边缘光颜色",  Color) = (0,0,0,0)
        _EdgeLightSize("边缘光区域大小", Float) = 0.1
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque" "Queue" = "Transparent+100"}
        LOD 200

        CGPROGRAM

        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows vertex:vert nofog

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _BumpMap;
        sampler2D _GMA;

        struct appdata_t
        {
            float4 vertex : POSITION; 
	        float4 tangent : TANGENT;  
	        float3 normal : NORMAL; 
	        float4 texcoord : TEXCOORD0;   
	        float4 texcoord1 : TEXCOORD1;
	        float4 color : COLOR; 
        };

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float2 uv_GMA;
            float3 worldPosition;
            float3 normal;
        };

        void vert(inout appdata_t v, out Input o)
        {
            UNITY_INITIALIZE_OUTPUT(Input, o);
            o.worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz;
            o.normal = v.normal;
        }

        half _Glossiness;
        half _Metallic;
        half4 _Color;
        half _BumpScale;

        half4 _EdgeLightColor;
        half _EdgeLightSize;
        half _EdgeMaxScale;

        void surf(Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 color = tex2D(_MainTex, IN.uv_MainTex);
            fixed4 normal = tex2D(_BumpMap, IN.uv_BumpMap);
            normal.xyz = normalize(UnpackNormalWithScale(normal, _BumpScale));
            fixed4 gma = tex2D(_GMA, IN.uv_GMA);

            half3 normalWorld = UnityObjectToWorldNormal(IN.normal.xyz);
            half3 eyeVec = normalize(IN.worldPosition.xyz - _WorldSpaceCameraPos);

            // 边缘光
            half NdotH = abs(dot(normalWorld, eyeVec));
            half Edge = saturate(NdotH / _EdgeLightSize);

            half3 EdgeColor = (1 - Edge) * _EdgeLightColor.rgb;
            color.rgb += color.rgb * EdgeColor + (half3(1, 1, 1) - color.rgb) * EdgeColor;

            color *= _Color;
            o.Albedo = color.rgb * gma.b;
            o.Metallic = _Metallic * gma.r;
            o.Smoothness = _Glossiness * gma.g;
            o.Alpha = color.a;
            o.Normal = normal.xyz;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
